<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Statistics extends Backend
{

    /**
     * Statistics模型对象
     * @var \app\admin\model\Statistics
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Statistics;

    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['app'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {

                $row->getRelation('app')->visible(['name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function echarts($startDay = null,  $field = null, $endDay=null)
    {
        $day = floor((strtotime($endDay)-strtotime($startDay))/86400);
        $beforDay = date('Y-m-d',strtotime("$startDay - $day day"));
        $startDay = empty($startDay) ? date('Y-m-d', time()) : $startDay;
        $endDay = empty($endDay) ? date('Y-m-d',strtotime('next month')) : $endDay;
        if ($this->request->isAjax()) {
            $field = empty($field ) ? 'day2' : $field;

            $option['legend'] = Db::name('app')->where('status', 0)->column('name');
            $option['xAxis'] = $this->model->Distinct(true)
                ->where('date','between time',[$startDay,$endDay])
                ->column('date');
            $count_date = count($option['xAxis']);
            //dump($option);
            $app_list = Db::name('app')->where('status',0)->field('app_id as id,name')->select();
            foreach ($app_list as $key => $val) {
                $option['series'][$key]['type'] = 'line';
                $option['series'][$key]['name'] = $val['name'];

                $option['series'][$key]['stack'] = '数量';
                //$data = $this->model->where('app_id',$val['id'])
                $option['series'][$key]['data'] = $this->model->where('app_id',$val['id'])
                    ->where('date','between time',[$startDay,$endDay])
                    //->field("$field")
                    ->order('date')
                    ->column($field);
               // $option['series'][$key]['data'] = [];
               /* foreach ($data as $k => $v) {
                    if (!empty( $v)) {
                        //$option['series']['data'][] = numbe ($val/$key)*100;
                        $option['series'][$key]['data'][] = number_format($v[$field]/$v['num'],4)*100;
                    } else {
                        $option['series'][$key]['data'][] = 0;
                    }

                }*/
                // dump($option['series'][$key]['data']);
                $count_series_date  = count($option['series'][$key]['data']);
                //判断某个app是否有相对应的日期天数总结，没有则填充0
                $count = $count_date - $count_series_date;
                if ( $count > 0) {
                    $option['series'][$key]['data'] = array_pad($option['series'][$key]['data'], -$count_date, 0);
                }
            }

            $now = $this->model->where('date','between time',[$startDay,$endDay])
                ->order('date')
                ->sum($field);

            return json($option);
        }

        $list = [
            'new' => '新增',
            'login' => '活跃',
            'advert' => '广告播放总次数',
            'advert_fill' => '广告总填充率',
            'advert1' => '激励广告播放总次数',
            'advert2_fill' => '激励填充率',
            'advert2' => '插屏广告播放总次数',
            'advert_fill' => '插屏填充率',
            'advert3' => 'banner广告播放总次数',
            'advert3_fill' => 'banner填充率',
            'advert4' => '积分墙广告播放总次数',
            'advert_fill' => '积分墙填充率'
        ];
        $this->view->assign("startDay", $startDay);
        $this->view->assign("endDay", $endDay);
        $this->view->assign("list", $list);
        return $this->view->fetch();
    }
    public function action() {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['app'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {

                $row->getRelation('app')->visible(['name']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    public function actionecharts($startDay = null,  $field = null, $endDay=null) {
        $startDay = empty($startDay) ? date('Y-m-d', time()) : $startDay;
        $endDay = empty($endDay) ? date('Y-m-d',strtotime('next month')) : $endDay;
        if ($this->request->isAjax()) {
            $field = empty($field ) ? 'day2' : $field;

            $option['legend'] = Db::name('app')->column('name');
            $option['xAxis'] = $this->model->Distinct(true)
                ->where('date','between time',[$startDay,$endDay])
                ->column('date');
            $count_date = count($option['xAxis']);
            //dump($option);
            $app_list = Db::name('app')->where('status',0)->field('app_id as id,name')->select();
            foreach ($app_list as $key => $val) {
                $option['series'][$key]['type'] = 'line';
                $option['series'][$key]['name'] = $val['name'];

                $option['series'][$key]['stack'] = '数量';
                //$data = $this->model->where('app_id',$val['id'])
                $option['series'][$key]['data'] = $this->model->where('app_id',$val['id'])
                    ->where('date','between time',[$startDay,$endDay])
                    //->field("$field")
                    ->order('date')
                    ->column($field);
                // $option['series'][$key]['data'] = [];
                /* foreach ($data as $k => $v) {
                     if (!empty( $v)) {
                         //$option['series']['data'][] = numbe ($val/$key)*100;
                         $option['series'][$key]['data'][] = number_format($v[$field]/$v['num'],4)*100;
                     } else {
                         $option['series'][$key]['data'][] = 0;
                     }

                 }*/
                // dump($option['series'][$key]['data']);
                $count_series_date  = count($option['series'][$key]['data']);
                //判断某个app是否有相对应的日期天数总结，没有则填充0
                $count = $count_date - $count_series_date;
                if ( $count > 0) {
                    $option['series'][$key]['data'] = array_pad($option['series'][$key]['data'], -$count_date, 0);
                }
            }
            return json($option);
        }

        $list = [
            'login' => '活跃用户人数',
            'sdk' => '进入sdk人数',
            'sign' => '签到人数',
            'double_sign' => '双倍签到人数',
            'apply' => 'paypal验证申请人数',
            'adopt' => 'paypal验证通过人数',
            'click_coin' => 'Cash out点击次数',
            'click_dollar' => 'Redeem点击次数',
            'click_cashout' => '提现申请数'
        ];
        $this->view->assign("startDay", $startDay);
        $this->view->assign("endDay", $endDay);
        $this->view->assign("list", $list);
        return $this->view->fetch();
    }
}
